package com.wangt.drools.controller;

import com.wangt.api.domain.DroolsRule;
import com.wangt.common.core.web.controller.BaseController;
import com.wangt.drools.config.DroolsManager;
import com.wangt.drools.service.IDroolsRuleService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/drools/rule")
@AllArgsConstructor
public class DroolsRuleController extends BaseController {


    private final IDroolsRuleService droolsRuleService;
    @Resource
    private final DroolsManager droolsManager;

    /**
     * 获取 执行规则列表 分页
     * @return
     */
    @GetMapping("findAll")
    public List<DroolsRule> findAll() {
        return droolsRuleService.findAll();
    }


    /**
     * 添加执行规则
     * @param droolsRule
     * @return
     */
    @PostMapping("add")
    public String addRule(@RequestBody DroolsRule droolsRule) {
        droolsRuleService.addDroolsRule(droolsRule);
        return "添加成功";
    }

    /**
     * 修改执行规则
     * @param droolsRule
     * @return
     */
    @PostMapping("update")
    public String updateRule(@RequestBody DroolsRule droolsRule) {
        droolsRuleService.updateDroolsRule(droolsRule);
        return "修改成功";
    }

    /**
     * 删除执行规则
     * @param ruleId
     * @param ruleName
     * @return
     */
    @PostMapping("deleteRule")
    public String deleteRule(Long ruleId, String ruleName) {
        droolsRuleService.deleteDroolsRule(ruleId, ruleName);
        return "删除成功";
    }

    /**
     * 调用执行规则对外接口
     * @param kieBaseName
     * @param param
     * @return
     */
    @GetMapping("fireRule")
    public String fireRule(String kieBaseName, Integer param) {
        return droolsManager.fireRule(kieBaseName, param);
    }


}
